﻿using System;
using System.Collections.Generic;
using System.Web.ModelBinding;
using System.Web.Mvc;
using PagedList;
using VietTrips.Domain.Models;
using VietTrips.Domain.Services;
using VietTrips.WebUI.Helpers;

namespace VietTrips.WebUI.Areas.Management.Controllers
{
    [Authorize(Roles = "Administrator, Agency")]
    public class ActivityManagerController : Controller
    {

        private readonly ActivityManagerService _activityManagerService = new ActivityManagerService();

        //
        // GET: /Management/ActivityManager/
        //
        public ActionResult ActivitiesList(int? page = 1,
            int? rowsPerPage = 20,
            string sortOrder = null,
            string currentFilter = null,
            string searchText = null)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.TypenameSortParm = String.IsNullOrEmpty(sortOrder) ? "ActivityName_desc" : "";

            if (searchText != null)
            {
                page = 1;
            }
            else
            {
                searchText = currentFilter;
            }
            ViewBag.CurrentFilter = searchText;
            List<Activity> viewModel = null;

            // Nếu có phân trang thì dựa vào số trang (pageNumber) và số dòng mỗi trang (rowsNumber), nếu không thì get hết
            viewModel = _activityManagerService.GetActivities(sortOrder);

            int pageNumber = (page ?? 1);
            int pageSize = (rowsPerPage ?? 20);
            ViewBag.Page = pageNumber;
            ViewBag.RowsPerPage = pageSize;

            return View(viewModel.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Management/ActivityManager/Details/5
        public ActionResult ActivityDetails(int id)
        {
            return RedirectToAction("Dashboard", "Admin");
        }

        //
        // GET: /Management/ActivityManager/Add

        public ActionResult NewActivity()
        {
            return View("NewActivity");
        }

        //
        // POST: /Management/ActivityManager/Create

        [HttpPost]
        public ActionResult NewActivity(FormCollection collection)
        {
            try
            {
                return RedirectToAction("ActivitiesList");
            }
            catch
            {
                return View("NewActivity");
            }
        }

        //
        // GET: /Management/ActivityManager/Edit/5

        public ActionResult UpdateActivity(int id)
        {
            return RedirectToAction("Dashboard", "Admin");
        }

        //
        // POST: /Management/ActivityManager/Edit/5

        [HttpPost]
        public ActionResult UpdateActivity(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Dashboard", "Admin");
            }
            catch
            {
                return RedirectToAction("Dashboard", "Admin");
            }
        }

        //
        // GET: /Management/ActivityManager/Delete/5

        public ActionResult DeleteActivity(int id)
        {
            return RedirectToAction("Dashboard", "Admin");
        }

        //
        // POST: /Management/ActivityManager/Delete/5

        [HttpPost]
        public ActionResult DeleteActivity(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Dashboard", "Admin");
            }
            catch
            {
                return RedirectToAction("Dashboard", "Admin");
            }
        }

        public ActionResult ActivityTypes(int? page = 1,
            int? rowsPerPage = 10,
            string sortOrder = null,
            string currentFilter = null,
            string searchText = null)
        {
            try
            {
                ViewBag.CurrentSort = sortOrder;
                ViewBag.TypenameSortParm = String.IsNullOrEmpty(sortOrder) ? "Typename_desc" : "";

                if (searchText != null)
                {
                    page = 1;
                }
                else
                {
                    searchText = currentFilter;
                }
                ViewBag.CurrentFilter = searchText;
                List<ActivityType> viewModel = null;
                
                // Nếu có phân trang thì dựa vào số trang (pageNumber) và số dòng mỗi trang (rowsNumber), nếu không thì get hết
                viewModel = _activityManagerService.GetTypes(sortOrder);

                int pageNumber = (page ?? 1);
                int pageSize = (rowsPerPage ?? 10);
                ViewBag.Page = pageNumber;
                ViewBag.RowsPerPage = pageSize;

                return View("ActivityTypes", viewModel.ToPagedList(pageNumber, pageSize));
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ManageTypes(int? page = 1,
            int? rowsPerPage = 10,
            string sortOrder = null,
            string currentFilter = null,
            string searchText = null,
            [Form] string[] checkboxes = null,
            [Form] string action = "")
        {
            return RedirectToAction("ActivityTypes", new
            {
                page = page,
                rowsPerPage = rowsPerPage,
                sortOrder = sortOrder,
                currentFilter = currentFilter,
                searchText = searchText
            });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddTypes([Form] string typeName, [Form] string typeDescription)
        {
            if (String.IsNullOrWhiteSpace(typeName))
            {
            }
            return RedirectToAction("ActivityTypes");
        }

        public ActionResult Movements()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddPlace()
        {
            return RedirectToAction("Places");
        }

        public ActionResult Places()
        {
            return View("Places");
        }

        public ActionResult Hotels()
        {
            return View("Hotels");
        }
    }
}
